package com.example.ass7.geometryprimitives;

/**
 * @author Meirav Ben Izhak.
 */
public class Velocity {
    // members
    private double dx;
    private double dy;

    /**
     * @param dx
     *            distance change on x axis
     * @param dy
     *            distance change on y axis
     */
    public Velocity(
        double dx, double dy) {
        this.dx = dx;
        this.dy = dy;
    }

    /**
     * @return dxToReturn .
     */
    public double getDx() {
        double dxToReturn = this.dx;
        return dxToReturn;
    }

    /**
     * @return dyToReturn.
     */
    public double getDy() {
        double dyToReturn = this.dy;
        return dyToReturn;
    }

    /**
     * @return double
     */
    public double getSpeed() {
        double xSquare = this.getDx()
                * this.getDx();
        double ySquare = this.getDy()
                * this.getDy();

        double speedToReturn = Math.sqrt(xSquare
                + ySquare);
        return speedToReturn;
    }

    /**
     * @param dyToSet
     *            receives the value to set.
     */
    public void setDy(double dyToSet) {
        this.dy = dyToSet;
    }

    /**
     * @param dxToSet
     *            receives the value to set.
     */
    public void setDx(double dxToSet) {
        this.dx = dxToSet;
    }

    /**
     * @param p
     *            p
     * @param speed
     *            speed
     * @param dt
     *            dt
     * @return Point
     */
    public Point applyToPoint(Point p,
            double speed,
            double dt) {
        double nextX = p.getX()
                + (this.dx * dt);
        double nextY = p.getY()
                + (this.dy * dt);
        Point nextPoint = new Point(nextX, nextY);
        return nextPoint;
    }

    /**
     * @param pAngle
     *            represents the direction.
     * @param pSpeed
     *            represents the speed.
     * @return the velocity in terms of x & y changes.
     */
    public static Velocity fromAngleAndSpeed(double pAngle,
            double pSpeed) {
        // convert the angle according to the assignment's requirements
        // double angle1 = pAngle + 90;
        // converts the angle from degrees to radians
        double angleInRad = pAngle
                * Math.PI / 180;
        // calculates the cosinus and sinus of the angle it received
        double cos = Math.cos(angleInRad);
        double sin = Math.sin(angleInRad);
        // calculates dx and dy based on straight angled triangle formulas
        double dx1 = cos
                * pSpeed;
        double dy1 = sin
                * pSpeed;

        return new Velocity(dx1, dy1);
    }
}